<?php

namespace backend\modules\marketing\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\marketing\models\Coupon;

/**
 * CouponSearch represents the model behind the search form about `backend\modules\marketing\models\Coupon`.
 */
class CouponSearch extends Coupon
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['coupon_id', 'coupon_name'], 'safe'],
            [['start_time', 'end_time', 'coupon_number'], 'integer'],
            [['coupon_money', 'coupon_mustmoney'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Coupon::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'start_time' => $this->start_time,
            'end_time' => $this->end_time,
            'coupon_number' => $this->coupon_number,
            'coupon_money' => $this->coupon_money,
            'coupon_mustmoney' => $this->coupon_mustmoney,
        ]);

        $query->andFilterWhere(['like', 'coupon_id', $this->coupon_id])
            ->andFilterWhere(['like', 'coupon_name', $this->coupon_name]);

        return $dataProvider;
    }
}
